package org.breathe.dynamic.thread.pool.core.notify.wechat;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.breathe.dynamic.thread.pool.core.notify.AbstractNotifier;
import org.breathe.thread.pool.common.constant.WechatNotifyConstant;
import org.breathe.thread.pool.common.entity.MarkdownReq;
import org.breathe.thread.pool.common.entity.NotifyPlatform;

import java.util.Objects;

/** 微信的抽象通知
 * @author: breathe
 * @createTime: 2025-04-13
 */
@Slf4j
public abstract class AbstractWechatNotifier extends AbstractNotifier {
    /**
     * 线程池发送消息
     */
    protected void doSend(NotifyPlatform platform, String text) {
        String serverUrl = WechatNotifyConstant.WECHAT_WEH_HOOK + platform.getUrlKey();
        MarkdownReq markdownReq = new MarkdownReq();
        markdownReq.setMsgtype("markdown");
        MarkdownReq.Markdown markdown = new MarkdownReq.Markdown();
        markdown.setContent(text);
        markdownReq.setMarkdown(markdown);
        HttpResponse response = null;

        try {
            log.info("JSON:{}", JSON.toJSONString(markdownReq));
            response = HttpRequest.post(serverUrl).body(JSON.toJSONString(markdownReq)).execute();
        } catch (Exception e) {
            log.error("breathe dynamicTp notify, wechat send message fail !!!", e);
        } finally {
            if (Objects.nonNull(response)) {
                log.info("breathe dynamicTp success, wechat send message success, response: {}, request: {}", response.body(), JSON.toJSONString(markdownReq));
            }
        }
    }
}
